package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomerException;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.DishFlavor;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    DishFlavorService dishFlavorService;

    @Override
    public void saveDishWithFlavor(DishDto dishDto) {

        //1.添加菜品信息
        this.save(dishDto);
        //2.添加菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishDto.getId());
        }
        //批量保存
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 查询菜品
     */
    @Autowired
    DishMapper dishMapper;

    @Override
    public Page<DishDto> pageDishKDto(Integer page, Integer pageSize, String name) {

        Page<DishDto> pageInfo = new Page<>(page, pageSize);
        return dishMapper.pageDishDto(pageInfo, name);
    }


    @Override
    public DishDto getDishByIdWithFlavor(Long id) {

        DishDto dishDto=dishMapper.getDishByIdWithFlavor(id);
        return dishDto;
    }

    @Override
    public void putById(DishDto dishDto) {
        this.updateById(dishDto);

        dishFlavorService.lambdaUpdate()
                .eq(DishFlavor::getDishId,dishDto.getId())
                .remove();

        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
flavor.setDishId(dishDto.getId());
        }
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public List<DishDto> listDishDto(Long categoryId, String name) {

        return dishMapper.listDishDto(categoryId,name);
    }




    /**
     * 根据ids删除菜品和菜品信息
     * @param ids
     */
    @Override
    public void deleteByIds(Long[] ids) {
        //1.判断是否停售，如果大于0就说明有在销售的不能删除
        //select count(*) from dish where id in (ids) and status=1;

        Integer count = this.lambdaQuery()
                .in(Dish::getId, ids)
                .eq(Dish::getStatus, 1)
                .count();
        if (count>0){
            throw new CustomerException("有菜品在销售，无法删除");
        }

        //2.删除菜品
        this.removeByIds(Arrays.asList(ids));
        //3.删除菜品信息

        dishFlavorService.lambdaUpdate()
                .in(DishFlavor::getDishId,ids)
                .remove();

    }


//根据套餐id查询菜品
    @Override
    public List<Dish> getDishBySetmealId(Long setmeal_id) {

        return dishMapper.getDishBySetmealId(setmeal_id);
    }
}


